/*
	Copyright (C) 2010 2011 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef GANTTLINK_H
#define GANTTLINK_H

#include <QGraphicsItem>
#include <QList>
#include <QLine>

#include <procycle/cycle.h>
#include <procycle/link.h>

#include "ganttitem.h"
#include "globals.h"
#include "pcchild.h"

class GanttCycle;
class GanttTask;

class GanttLink : public GanttItem
{
public:
	GanttLink(ProCycle* app, GanttCycle* gCycle, PC::Link* link);
	virtual ~GanttLink();


	PC::Link* link() {
		return m_link;
	}

	void resetLink(PC::Link* link) {
		m_link = link;
	}

	GanttTask* gtFrom() {
		return m_gtFrom;
	}

	GanttTask* gtTo() {
		return m_gtTo;
	}


	void setCycleProp(PC::Cycle::Property p, const QVariant& value);
	void editModeChanged();
	void syncToExpansion();


	QRectF boundingRect() const;

	void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*);
	void paintLink(QPainter* painter);
	void paintFromDock(QPainter* painter);
	void paintToDock(QPainter* painter);

	bool pointOnLink(const QPointF& p);
	bool pointGrabsLag(const QPointF& p);

	bool pointCanHover(const QPointF &point) {
		return pointOnLink(point);
	}
	bool pointCanSelect(const QPointF &point) {
		return pointOnLink(point);
	}

	virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
	virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
	virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
	virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
	virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

signals:
	void linkSelected(PC::Link*);

protected:
	void requestSelection(bool selected=true);

private:
	PC::Link*		m_link;

	GanttTask*		m_gtFrom;
	GanttTask*		m_gtTo;

	QPointF			m_pFrom;
	QPointF			m_pTo;

	QRectF			m_bounds;
	QList<QLine>	m_lines;

	bool			m_drawCriticalPath;
	bool			m_drawCycleOverlap;

	bool			m_paintFromDock;
	bool			m_paintToDock;

	float			m_halfStep;
};

#endif // GANTTLINK_H
